package com.tenton.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenton.MyException.DataExist;
import com.tenton.MyException.DataNotExist;
import com.tenton.mapper.SupplierMapper;
import com.tenton.pojo.Supplier;
import com.tenton.service.SupplierService;
import com.tenton.utils.Constants;
import com.tenton.utils.IntegerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    SupplierMapper supplierMapper;

    /**
     * 删除供应商信息
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByPrimaryKey(int id) {
        return supplierMapper.deleteByPrimaryKey(id);
    }

    /**
     * 新增供应商信息
     *
     * @param supplier
     * @param session
     * @return
     */
    @Override
    public int insert(Supplier supplier, HttpSession session) throws DataExist, DataNotExist {
        if (supplier == null) {
            throw new DataNotExist("供应商信息为空");
        } else {
            String name = (String) session.getAttribute("name");
            Supplier params = supplierMapper.selectByParams(supplier.getSupName(), supplier.getPerson(), supplier.getEmail());
            if (params == null) {
                supplier.setCreateName(name);
                Date date = new Date();
                supplier.setCreateTime(date);
                return supplierMapper.insert(supplier);
            } else {
                throw new DataExist("供应商信息已存在");
            }
        }
    }

    /**
     * 获取所有供应商信息
     *
     * @param pn
     * @param model
     */
    @Override
    public void getAll(Integer pn, ModelMap model) {
        PageHelper.startPage(pn, Constants.PAGE_SIZE);
        List<Supplier> supplys = supplierMapper.selectAll();
        PageInfo<Supplier> pageInfo = new PageInfo<Supplier>(supplys);
        model.addAttribute("pageInfo", pageInfo);
    }

    /**
     * 查询供应商信息
     *
     * @param id
     * @param model
     * @return
     */
    @Override
    public void selectByPrimaryKey(Integer id, Model model) {
        if (IntegerUtils.isEmpty(id)) {
            throw new NullPointerException("供应商Id为空");
        } else {
            model.addAttribute("supply", supplierMapper.selectByPrimaryKey(id));
        }
    }

    /**
     * 修改供应商信息
     *
     * @param supplier
     * @param model
     * @param session
     * @return
     */
    @Override
    public boolean update(Supplier supplier, Model model, HttpSession session) throws DataNotExist {
        if (supplier == null){
            throw new DataNotExist("供应商信息为空");
        }else {
            String name = (String) session.getAttribute("name");
            Date date = new Date();
            supplier.setUpdateName(name);
            supplier.setUpdateTime(date);
            boolean flag = supplierMapper.updateByPrimaryKey(supplier);
            if (flag) {
                Supplier supply = supplierMapper.selectByPrimaryKey(supplier.getId());
                model.addAttribute("supply", supply);
            }
            return flag;
        }
    }

    /**
     * 供应商名称模糊查询
     *
     * @param supName
     * @param pn
     * @param model
     * @return
     */
    @Override
    public void getByParams(String supName, Integer pn, Model model) {
        PageHelper.startPage(pn, Constants.PAGE_SIZE);
        List<Supplier> supplys = supplierMapper.getByParams(supName);
        PageInfo<Supplier> pageInfo = new PageInfo<Supplier>(supplys);
        model.addAttribute("pageInfo", pageInfo);
    }
}
